package com.campus.service.Impl;

import com.campus.domain.Dto.StudentBatchAddDto;
import com.campus.domain.Dto.StudentCreateDto;
import com.campus.domain.Dto.UserRegisterDto;
import com.campus.domain.Vo.StudentExamineBatchVo;
import com.campus.mapper.StudentMapper;
import com.campus.mapper.UserMapper;
import com.campus.service.IStudentService;
import com.campus.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public Integer addStudent(StudentCreateDto studentCreateDto) {

        Long loginId = userMapper.addUser(studentCreateDto.getUserRegisterDto());

        return studentMapper.addStudent(studentCreateDto,loginId);
    }

    @Override
    public Integer examineStudent(Long id) {
        return studentMapper.examineStudent(id);
    }

    @Override
    public Integer addBatchStudent(List<StudentBatchAddDto> list) {

        List<StudentCreateDto> list1 = list.stream().map(studentBatchAddDto -> {
            StudentCreateDto student = new StudentCreateDto();

            UserRegisterDto user = new UserRegisterDto();
            user.setLoginName(studentBatchAddDto.getLoginName());
            user.setPassword(studentBatchAddDto.getPassword());
            user.setGender(studentBatchAddDto.getGender());
            user.setBirthday(studentBatchAddDto.getBirthday());
            user.setPhone(studentBatchAddDto.getPhone());
            user.setEmail(studentBatchAddDto.getEmail());
            List<Long> roleId = new ArrayList<>();
            roleId.add(4l);
            user.setRoleId(roleId);

            student.setUserRegisterDto(user);
            student.setStuName(studentBatchAddDto.getStuName());
            student.setFromPlace(studentBatchAddDto.getFromPlace());
            student.setParentPhone(studentBatchAddDto.getParentPhone());
            student.setCurrentGrade(studentBatchAddDto.getCurrentGrade());
            student.setIsFirst(studentBatchAddDto.getIsFirst());
            student.setComment(studentBatchAddDto.getComment());

            return student;
        }).collect(Collectors.toList());

        int rs = 0;
        for (StudentCreateDto studentCreateDto:list1){
            rs = addStudent(studentCreateDto);
        }
        return rs;
    }

    @Override
    public Integer examineStudentBatch(List<StudentExamineBatchVo> list) {

        int rs = 0;
        for(StudentExamineBatchVo studentExamineBatchVo:list){
            System.out.println(studentExamineBatchVo.getStatus());
            if(studentExamineBatchVo.getStatus().equals("1")){
                System.out.println(studentExamineBatchVo.getId());
                rs = examineStudent(studentExamineBatchVo.getId());
            }
        }
        return rs;
    }

    @Override
    public List<StudentExamineBatchVo> getStudentForExamine() {
        return studentMapper.selectForExamine();
    }


}
